#[cfg(feature = "axstd")]
use axstd::println;

use axhal::gpio::*;

pub struct LEDCtr {
    status: bool,
    gpio_system: Option<GpioSystem>,
}

impl LEDCtr {
    pub fn new() -> Self {
        Self {
            status: false,
            gpio_system: None,
        }
    }

    pub fn setup(&mut self) {
        let mut gpio_system = gpio::GpioSystem::new();
        let gpio0 = gpio_system.controller(0).unwrap();
        let p = gpio::GpioPins::p8;
        gpio0.set_pin_dir(p, true);

        self.gpio_system = Some(gpio_system)
    }

    fn set_pin_data(&mut self, data: bool) {
        let gpio0 = self.gpio_system.as_mut().unwrap().controller(0).unwrap();
        let p = gpio::GpioPins::p8;
        gpio0.set_pin_data(p, data);
        println!("current data: {data}");
    }

    pub fn enable(&mut self) {
        if self.status {
            return;
        }
        self.status = true;
        self.set_pin_data(true);
    }

    pub fn disable(&mut self) {
        if !self.status {
            return;
        }
        self.status = false;
        self.set_pin_data(false);
    }
}
